using Assets.Scripts.GDK;
using UnityEngine;
using UnityEngine.UI;

namespace Brick
{
	public class StoreItem : gdk_base_layer
	{
		public Text textName;

		public Text textSize;

		public Sprite[] images;

		public Image unUseImage;

		public Image imageBall;

		private int ballIndex;

		public string storeName;

		public string textDecript;

		private int isUsed;

		public Button unUse;   //未使用

		public Button buttonUse;  //当前使用

		public Button gaivn; //未解锁

		public Image gaivnIcon;

		public Text gaivnNum;

		private PiFuModel pfInfo;

		public void OnData(int splitIndex, PiFuModel pifumd, string strSize, int curIndex)
		{
			pfInfo = pifumd;
			if (!GameManager.getFuncOpen("pifuad"))
			{
				pfInfo.buyType = 2;
				pfInfo.need = 1000;
			}
			unUse.GetComponent<Transform>().gameObject.SetActive(value: false);
			buttonUse.GetComponent<Transform>().gameObject.SetActive(value: false);
			gaivn.GetComponent<Transform>().gameObject.SetActive(value: false);
			if (!GDK.ModuleManger.Module<StorePanelModule>().haveBall(splitIndex))
			{
				//未拥有
				gaivn.GetComponent<Transform>().gameObject.SetActive(value: true);
				//if (pfInfo.buyType == 8)
				//{
				//	gaivnNum.text = string.Empty;
				//	gaivnIcon.sprite = gdk_game_manager.loadSprite("UI/Shop/store_j_kgg");
				//	gaivnIcon.SetNativeSize();
				//	gaivnIcon.transform.localPosition = Vector2.zero;
				//}
				//else
				//{
				//	if (pfInfo.buyType == 1)
				//	{
				//		gaivnIcon.sprite = gdk_game_manager.loadSprite("UI/Common/common_xxx");
				//	}
				//	gaivnNum.text = pfInfo.need.ToString();
				//}
			}
			else if (splitIndex == curIndex)
			{
				//已拥有，并使用
				isUsed = 1;
				unUse.GetComponent<Transform>().gameObject.SetActive(value: true);
			}
			else
			{
				//已拥有，未使用
				isUsed = 0;
				buttonUse.GetComponent<Transform>().gameObject.SetActive(value: true);
			}
			ballIndex = splitIndex;
			storeName = pfInfo.name;
			textDecript = strSize;
			if ((bool)textName)
			{
				textName.text = storeName;
			}
			if ((bool)textSize)
			{
				textSize.text = textDecript;
			}
			if (isUsed == 1)
			{
			}
			imageBall.sprite = images[ballIndex];
		}

		public void OnUseClick()
		{
			gdk_game_manager.PlaySound(AudioID.click);
			isUsed = 1;
			GameData.useBallIndex = ballIndex;
			buttonUse.GetComponent<Transform>().gameObject.SetActive(value: false);
			unUse.GetComponent<Transform>().gameObject.SetActive(value: true);
			gdk_game_manager.DispachEvent(EventID.BALLCHANGE);
		}

		public void ReUsed()
		{
			if (ballIndex != GameData.useBallIndex && isUsed == 1)
			{
				buttonUse.GetComponent<Transform>().gameObject.SetActive(value: true);
				unUse.GetComponent<Transform>().gameObject.SetActive(value: false);
			}
		}

		/// <summary>
		/// 购买点击事件
		/// </summary>
		public void OnBuyClick()
		{
            //OnGainBall();	
            AdManager.Instance.ShowRewardedVideoAd(OnAdWatched, OnAdError);		
        }

        private void OnGainBall()
		{
			GDK.ModuleManger.Module<StorePanelModule>().OnGavinBall(ballIndex);
			gaivn.GetComponent<Transform>().gameObject.SetActive(value: false);
			isUsed = 1;
			GameData.useBallIndex = ballIndex;
			buttonUse.GetComponent<Transform>().gameObject.SetActive(value: false);
			unUse.GetComponent<Transform>().gameObject.SetActive(value: true);
			gdk_game_manager.DispachEvent(EventID.BALLCHANGE);
		}


        private void OnAdWatched()
        {
            OnGainBall();
            Debug.Log("广告观看完成");
        }

        private void OnAdError(string errorMessage)
        {
            Debug.LogError($"广告播放失败: {errorMessage}");
            // 加载 AdError 预制体
            GameObject adErrorPrefab = Resources.Load<GameObject>("AdError");
            if (adErrorPrefab != null)
            {
                // 实例化 AdError
                GameObject adErrorInstance = Instantiate(adErrorPrefab, transform.position, Quaternion.identity);
                // 1 秒后销毁实例
                Destroy(adErrorInstance, 1.5f);
            }
            else
            {
                Debug.LogError("AdError 预制体未找到，请确保其位于 Resources 文件夹中。");
            }
        }
    }
}

